package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.AbnormalAttendanceApply;
import com.amsp.oa.oademo.service.AbnormalAttendanceApplyService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/abnormal-attendance-apply")
@Api(tags = "异常情况出勤签卡申请单接口")
public class AbnormalAttendanceApplyController {

    @Autowired
    private AbnormalAttendanceApplyService abnormalAttendanceApplyService;

    @PostMapping("/save")
    @ApiOperation("保存异常考勤申请")
    public ResponseEntity<List<AbnormalAttendanceApply>> save(@RequestBody Map<String, Object> params) {
        List<Map<String, Object>> attendanceRecords = (List<Map<String, Object>>) params.get("attendanceRecords");
        Map<String, Object> formData = (Map<String, Object>) params.get("formData");

        if (!attendanceRecords.isEmpty()) {
            List<AbnormalAttendanceApply> applies = new ArrayList<>();

            for (Map<String, Object> record : attendanceRecords) {
                AbnormalAttendanceApply apply;
                // 检查是否有ID，有则获取已存在的记录
                Object idObj = record.get("id");
                if (idObj != null) {
                    Long id = Long.valueOf(idObj.toString());
                    apply = abnormalAttendanceApplyService.getById(id);
                    if (apply == null) {
                        apply = new AbnormalAttendanceApply();
                    }
                } else {
                    apply = new AbnormalAttendanceApply();
                }

                apply.setName((String) record.get("name"));
                apply.setEmployeeId((String) record.get("employeeId"));
                apply.setTeam((String) record.get("team"));
                apply.setMorning((String) record.get("morning"));
                apply.setAfternoon((String) record.get("afternoon"));
                apply.setOvertime((String) record.get("overtime"));
                apply.setAbnormalSituation((String) record.get("abnormalSituation"));
                apply.setRemark((String) record.get("remark"));
                apply.setDate(LocalDate.parse((String) record.get("date")));

                apply.setTabledBy((String) formData.get("tabledBy"));
                apply.setReviewedBy((String) formData.get("reviewedBy"));
                apply.setApprovedBy((String) formData.get("approvedBy"));

                // 如果是新记录，设置创建时间
                if (apply.getCreateTime() == null) {
                    apply.setCreateTime(LocalDateTime.now());
                }
                apply.setUpdateTime(LocalDateTime.now());

                applies.add(apply);
            }

            abnormalAttendanceApplyService.saveOrUpdateBatch(applies);
            return ResponseEntity.ok(applies);
        }
        return ResponseEntity.badRequest().build();
    }

    @GetMapping("/list")
    @ApiOperation("获取异常考勤申请列表")
    public ResponseEntity<IPage<AbnormalAttendanceApply>> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String employeeId,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String team) {
        return ResponseEntity.ok(abnormalAttendanceApplyService.getPage(current, size, employeeId, name, team));
    }

    @GetMapping("/{id}")
    @ApiOperation("获取异常考勤申请详情")
    public ResponseEntity<AbnormalAttendanceApply> getById(@PathVariable Long id) {
        return ResponseEntity.ok(abnormalAttendanceApplyService.getById(id));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除异常考勤申请")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        abnormalAttendanceApplyService.removeById(id);
        return ResponseEntity.ok().build();
    }
}